﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RATSharp.Model;
using RATSharp.Rest;
using System.Threading.Tasks;
namespace RATSharpW7.ViewModel
{
    public class DayViewModel : CollectionViewModel<Period, Periods>
    {
        private DateTime selectedDate;

        public DayViewModel(DateTime selectedDate)
        {
            this.selectedDate = selectedDate;
        }

        public DateTime SelectedDate
        {
            get
            {
                return selectedDate;
            }
        }

        public void loadPeriods(DateTime firstDayInMonth, DateTime LastDayInMonth)
        {
            Task<Reservations> task = RestService.getInstance().getReservationsByUserAsync(firstDayInMonth, LastDayInMonth);
            notifyChangeModel(task);
        }

        protected void notifyChangeModel(Task<Reservations> task)
        {
            task.ContinueWith(t =>
            {
                Reservations resList = t.Result;

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (resList != null)
                    {
                        ReservationFactory.MakeBidirectionalConnection(resList);
                        this.loadedObjectsDataSource.Clear();
                        foreach (Reservation res in resList)
                        {
                            foreach (Period per in res.periods)
                            {
                                this.loadedObjectsDataSource.Add(per);
                            }
                        }
                    }
                });
            });
        }
    }
}
